/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import pl.cezary.butler.walili.client.gui.GUI;
import pl.cezary.butler.walili.client.LoginControler;
import pl.cezary.butler.walili.client.UserDataSource;
import pl.cezary.butler.walili.common.ILoginFacadeRemote;
import pl.cezary.butler.walili.common.WaliLiSecurityManager;

/**
 *
 * @author Cezary Butler
 */
public class ServicesLocatorRMI implements IServicesLocator {
    
    private static final Logger log = Logger.getLogger(ServicesLocatorRMI.class.getName());
    private final UserDataSource userDs;
    
    public static IServicesLocator getInstance(UserDataSource ds){
        return new ServicesLocatorRMI(ds);
    }
    
    private ServicesLocatorRMI(UserDataSource ds) {
        userDs = ds;
    }       
    
    @Override
    public ILoginFacadeRemote getLoginFacade(){ 
        final String HOST = userDs.getHost();
        try {
            Registry rejestr = LocateRegistry.getRegistry(HOST, WaliLiSecurityManager.PORT);
            return (ILoginFacadeRemote) rejestr.lookup(ILoginFacadeRemote.NAME);
            
        } catch (NotBoundException ex) { // 
            Logger.getLogger(LoginControler.class.getName()).log(Level.SEVERE, null, ex);                     
            userDs.alert(GUI.getLocalized("loginCtl.no_host")+HOST);
        } catch (AccessException ex) {
            Logger.getLogger(LoginControler.class.getName()).log(Level.SEVERE, null, ex);            
            userDs.alert(GUI.getLocalized("loginCtl.no_access_to_host")+HOST);
        } catch (RemoteException ex) {            
            userDs.alert(GUI.getLocalized("loginCtl.no_host")+HOST);
            Logger.getLogger(LoginControler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    
}
